package com.easy.user.controller;

import java.sql.ResultSet;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson2.JSON;
import com.easy.bean.Address;
import com.easy.bean.Users;
import com.easy.common.CommonResult;
import com.easy.exception.NoPassException;
import com.easy.exception.NoUserException;
import com.easy.user.service.IUserService;
import com.easy.user.util.HttpUtil;
import com.easy.user.util.JWTUtil;

@RestController
@RequestMapping("user")
@CrossOrigin
public class EasyUserController {
	@Autowired
	IUserService userS;

	@RequestMapping("test")
	public CommonResult test() {
		ResultSet rs = null;
		return CommonResult.success("test OK 8821");
	}

	@PostMapping("/dologin")
	public CommonResult login(Users user) {
		String token = null;
		try {
			token = userS.login(user);
		} catch (NoPassException | NoUserException e) {
			return CommonResult.failed(401, e.getMessage());
		}
		return CommonResult.success(token);
	}

	@PostMapping("/loginout")
	public CommonResult login(HttpServletRequest request) {
		return CommonResult.success();
	}

	@GetMapping("/userinfo")
	public CommonResult userInfo(HttpServletRequest request) {
		String token = request.getHeader("Authorization");
		Integer id = JWTUtil.userid(token);
		// 查询该用户的信息--- 嵌入redis
		Users user = userS.getUsersByID(id);
		user.setPassword(null);
		return CommonResult.success(user);
	}

	@PutMapping("/avatar")
	public CommonResult editAvatar(HttpServletRequest request, String avatar) {
		String token = request.getHeader("Authorization");
		Integer id = JWTUtil.userid(token);
		userS.editAvatar(id, avatar);
		return CommonResult.success();
	}

	@GetMapping("address")
	public CommonResult getAddressList(HttpServletRequest request) {
		String token = request.getHeader("Authorization");
		Integer id = JWTUtil.userid(token);
		List<Address> list = userS.getAddressList(id);
		return CommonResult.success(list);
	}

	@PostMapping("address")
	public CommonResult insertAddress(HttpServletRequest request, @RequestBody Address address) {
		String token = request.getHeader("Authorization");
		Integer id = JWTUtil.userid(token);
		address.setUserid(id);
		int result = userS.insertAddress(address);
		return CommonResult.success(result);
	}

	@PutMapping("address/default")
	public CommonResult setAddressDefault(HttpServletRequest request, int id) {
		String token = request.getHeader("Authorization");
		Integer userid = JWTUtil.userid(token);

		userS.setAddressDefault(userid, id);
		return CommonResult.success();
	}

	@DeleteMapping("address/{id}")
	public CommonResult delAddress(@PathVariable int id) {
		userS.deleteAddress(id);
		return CommonResult.success();
	}

	@GetMapping("area")
	public CommonResult getArea(String keyword) {
		String url = "https://restapi.amap.com/v3/config/district?key=d5e466c74f37e2b1f89163a68a161ea7&keywords="
				+ keyword;
		String result = HttpUtil.get(url);
		HashMap map = JSON.parseObject(result);
		return CommonResult.success(map);
	}

}